$(document).ready(function(){
	$('#lab_name').blur(function(){
		isNotEmpty($('#lab_name'),_("Empty test name"),"statusMessageLab");
	});
	$('#lab_description').blur(function(){
		isNotEmpty($('#lab_description'),_("Empty test result"),"statusMessageLab");
	});
});

function viewLaboratories() {
	$.ajax ({
		type: 	"GET",
		url:	"viewLaboratoryAjax?set_ajax_view",
		data:	"laboratory_id="+$('#lab_id').val(),
		dataType: "json",
		success: function(data) {
			$('#patient_id').val(data.patient['doctor_id']);
			$('#patient_name').text(data.patient['patient_name']);
			$('#doctor_id').val(data.doctor['doctor_id']);
			$('#doctor_name').text(data.doctor['doctor_name']);
			$('table#laboratories tbody').html(data.laboratoryItems);
			$('table#laboratories tr:even').addClass('alt');
		}
	});
}

function isReadyToAddLabItem(){
	if(!isNotEmpty($('#lab_name'),_("Empty test name"),"statusMessageLab")){
		return false;
	}
	if(!isNotEmpty($('#lab_description'),_("Empty test result"),"statusMessageLab")){
		return false;
	}
	return true;
}

function addLaboratoryItem() {
	if(!isReadyToAddLabItem()){
		return;
	}
	$('#addLabBtn').css('display', 'none');
	$('#loading').css('display', 'block');
	var submitData = $('#addLabForm').formSerialize();
	$.ajax ({
		type: 	"POST",
		url:	"../Laboratory/addLaboratoryItemAjax?set_ajax_view",
		data:	submitData,
		dataType: "json",
		success: function(data) {
			$('#loading').css('display', 'none');
			$('#addLabBtn').css('display', 'inline');
			if(data.saved){
				$('#lab_id').val(data['lab_id']);
				$('table#laboratories tr#noresult').remove();
				$('table#laboratories tbody').append(data.labItem);
				$('table#laboratories tr:even').addClass('alt');
			}
			else{
				if(data.message=="Data duplicated"){
					$('span#statusMessageLab').text(_("Duplicated data! Please input another")).show();
				}
			}
			$('#lab_name').val('');
			$('#lab_description').val('');
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$('#loading').css('display', 'none');
			$('#addLabBtn').css('display', 'inline');
			$('span#statusMessageLab').text("Request errors: " + XMLHttpRequest.responseText).show();
		}
	});
}

function deleteLabItem(id){
	$.ajax({
		type: 	"GET",
		url:	"../Laboratory/deleteLaboratoryItemAjax?set_ajax_view",
		data: 	"labItemId=" + id,
		dataType:	"json",
		success: function(data){
			if(data.deleted){
				removeLabItemRow(id);
				$('span#statusMessageLab').text('').hide();
			}
			else{
				showMesage("Xóa không thành công",'statusMessageLab');
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$('#loading').css('display', 'none');
			$('#addLabBtn').css('display', 'inline');
			$('span#statusMessageLab').text("Request errors: " + XMLHttpRequest.responseText).show();
		}
	});
}

function removeLabItemRow(id){
	$("#" + id).parent().parent().remove();
	$('table#laboratories tr:even').addClass('alt');
	$('table#laboratories tr:odd').removeClass('alt');
	if ($('table#laboratories tbody tr').length == 0) {
		$('table#laboratories tbody').append("<tr id='noresult'><td colspan='5'>" + _("None of test info and clinical examination is available") + "</td></tr>");
	}
}

function showMessage(mesage, target){
	$("#"+target).text(message).show();
}

function editLabItem(itemId) {
	var editClass = 'editFields' + itemId;
	$('#oldName_' + itemId).hide();
	$('#oldDesc_' + itemId).hide();
	$('#newName_' + itemId).val($('#oldName_' + itemId).text());
	var newDescription = $('#oldDesc_' + itemId).html();
	while ((newDescription.match("<br>") != null) || newDescription.match("<BR>") != null) {
		newDescription = newDescription.replace(/<br>/, "\n");
		newDescription = newDescription.replace(/<BR>/, "\n");
	}
	$('#newDesc_' + itemId).val(newDescription);
	$('#newName_' + itemId).addClass(editClass);
	$('#newDesc_' + itemId).addClass(editClass);
	$('#newName_' + itemId).parent('span').show();
	$('#newDesc_' + itemId).parent('span').show();
	$('#edit_' + itemId).hide();
	$('#save_' + itemId).show();
	$('#cancel_' + itemId).show();
}

function cancelLabItem(itemId) {
	var editClass = 'editFields' + itemId;
	$('#newName_' + itemId).val('');
	$('#newDesc_' + itemId).val('');
	$('#newName_' + itemId).parent('span').hide();
	$('#newDesc_' + itemId).parent('span').hide();
	$('#newName_' + itemId).removeClass(editClass);
	$('#newDesc_' + itemId).removeClass(editClass);
	$('#oldName_' + itemId).show();
	$('#oldDesc_' + itemId).show();
	$('#save_' + itemId).hide();
	$('#cancel_' + itemId).hide();
	$('#edit_' + itemId).show();
}

function saveLabItem(itemId) {
	if ( ($.trim($('#newName_' + itemId).val()) == '') || 
	     ($.trim($('#newDesc_' + itemId).val()) == '') ) {
		cancelLabItem(itemId);
	} else {
		var editClass = 'editFields' + itemId;
		var submitData = $('#editLabForm .' + editClass).fieldSerialize();
		$.ajax ({
			type: 	"POST",
			url:	"../Laboratory/editLaboratoryItemAjax?set_ajax_view",
			data:	submitData + "&lab_item_id=" + itemId,
			dataType: "json",
			success: function(data) {
				if (data.saved == 1) {
					$('#oldName_' + itemId).html(data.labName);
					$('#oldDesc_' + itemId).html(data.labDesc);
					$('#oldDesc_' + itemId).parent('td').next().html(data.doctor);
					$('#oldDesc_' + itemId).parent('td').next().next().html(data.updated);
				} else if (data.message == 'Data duplicated') {
					$('span#statusMessageLab').text(_("Duplicated data! Please input another")).show();
				}
				cancelLabItem(itemId);
			}
		});
	}
}